package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.user.IKaptchaService;
import com.mall.user.UserSerivce;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.CheckUserLoginResponse;
import com.mall.user.dto.KaptchaCodeRequest;
import com.mall.user.dto.KaptchaCodeResponse;
import com.mall.user.dto.UserLoginResponse;
import io.swagger.models.auth.In;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RequestMapping("/user")
@RestController

public class UserLoginController {

    @Reference(interfaceClass = UserSerivce.class,check = false)
    UserSerivce userSerivce;

    @Reference(interfaceClass = IKaptchaService.class,check = false)
    IKaptchaService kaptchaService;



    @PostMapping("login")
    @Anoymous
    public ResponseData userLogin(@RequestBody Map map, HttpServletRequest request, HttpServletResponse response){
            UserLoginResponse userLoginResponse = userSerivce.userLogin(map);
            if(!userLoginResponse.getCode().equals("000000")){
                ResponseData data = new ResponseUtil().setErrorMsg("用户名或密码不正确");
                return data;
            }


        //获得验证码的uuid
        String kaptcha_uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        //创一个对象用来校验验证码是否正确
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        kaptchaCodeRequest.setCode((String) map.get("captcha"));
        kaptchaCodeRequest.setUuid(kaptcha_uuid);

        //进行验证验证码
        KaptchaCodeResponse kaptchaCodeResponse = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if(!kaptchaCodeResponse.getCode().equals("000000")){
            ResponseData data = new ResponseUtil().setErrorMsg("验证码不正确鸭");
            return data;
        }


        ResponseData data = new ResponseUtil().setData(userLoginResponse, "success");

        Cookie cookie = CookieUtil.genCookie("access_token", userLoginResponse.getToken(), "/", 60*60*24);
        response.addCookie(cookie);

        return data;
    }
    //todo
    @GetMapping("login")
    public ResponseData checkUserLogin(HttpServletRequest request){
        Integer userId = Integer.parseInt((String) request.getAttribute("userInfo"));
        String access_token = CookieUtil.getCookieValue(request, "access_token");
        if (access_token==null){
            return new ResponseUtil<>().setErrorMsg("token已失效");
        }
        CheckUserLoginResponse checkUserLoginResponse =  userSerivce.checkUserLogin(userId);

        return new ResponseUtil<>().setData(checkUserLoginResponse, ShoppingRetCode.SUCCESS.getMessage("000000"));
    }


}
